package com.fau.wm.action.index;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.wm.action.PublicAction;
import com.fau.wm.entity.MedicalAdmin;
import com.fau.wm.entity.MedicalInventory;
import com.fau.wm.entity.MedicalInventoryDetail;
import com.fau.wm.util.SystemConfig;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 库存盘点Action
 * @author kangyj
 * @datetime 2014年8月15日,下午5:18:52
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class InventoryAction extends PublicAction implements ModelDriven<MedicalInventory> {

	private static final long serialVersionUID = -1113390542686652230L;
	
	private MedicalInventory inventory = new MedicalInventory();
	
	/**
	 * @desc 盘点列表初始化
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月15日,下午5:44:44
	 */
	@Action(value = "inventory_init", results = {@Result(name = SUCCESS, location = "inventory_init.jsp")})
	public String init(){
		return SUCCESS;
	}
	
	/**
	 * @desc 查询盘点记录
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月15日,下午5:45:13
	 */
	@Action(value = "inventory_view")
	public String view(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ServletActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		inventory.setHospitalId(admin.getEnterpriseId());
		pageBean = this.baseAllService.getInventoryManagerService().findInventoryByPage(inventory, map);
		map.clear();
		map.put("total", pageBean.getTotalRow());
		map.put("data", pageBean.getResult());
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 添加盘点记录基本信息
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月15日,下午5:47:03
	 */
	@Action(value = "inventory_add", results = {@Result(name = SUCCESS, location = "inventory_add.jsp")})
	public String add(){
		return SUCCESS;
	}
	
	/**
	 * @desc 保存盘点记录基本信息
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月15日,下午5:47:37
	 */
	@Action(value = "inventory_save")
	public String save(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		boolean flag = this.baseAllService.getInventoryManagerService().saveInventory(inventory, admin);
		if(flag){
			map.put("success", true);
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 验证输入编码是否已存在 
	 * @return
	 * @author suny
	 * @datetime 2014年9月4日,下午3:13:21
	 */
	@Action(value="inventory_ver_serialnum")
	public String verSerialNum(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if(StringUtils.isNotBlank(inventory.getSerialNum())){
			Boolean flag = this.baseAllService.getInventoryManagerService().verSerialNum(inventory.getSerialNum(), admin.getEnterpriseId());
			if(flag){
				map.put("result", true);
			}else{
				map.put("result", false);
			}
		}else {
			map.put("result", "error");
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 修改盘点基本信息
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月15日,下午5:48:56
	 */
	@Action(value = "inventory_edit", results = {@Result(name = SUCCESS, location = "inventory_edit.jsp")})
	public String edit(){
		return SUCCESS;
	}
	
	/**
	 * @desc 保存修改盘点基本信息
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月15日,下午5:53:12
	 */
	@Action(value = "inventory_save_edit")
	public String saveEdit(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		
		if(StringUtils.isNotBlank(inventory.getId())){
			this.baseAllService.getInventoryManagerService().saveEdit(inventory);
			map.put("success", true);
			}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 删除盘点记录
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月15日,下午5:55:02
	 */
	@Action(value = "inventory_del")
	public String del(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(inventory.getId())){
			this.baseAllService.getInventoryManagerService().delInventory(inventory);
			map.put("success", true);
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 查询仓库集合
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月16日,下午12:22:03
	 */
	@Action(value = "inventory_find_warehouse")
	public String findWarehouseList() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getBaseDataService().findWarehouseByHospital(admin.getEnterpriseId());
		formatJSON(response, list);
		return null;
	}

	/**
	 * @desc 查看盘点单详细
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月11日,下午3:23:06
	 */
	@Action(value = "inventory_data_view", results = {@Result(name = SUCCESS, location = "inventory_data_view.jsp")})
	public String dataView(){
		return SUCCESS;
	}
	
	/**
	 * @desc 盘点审核页面初始化
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月20日,上午12:08:10
	 */
	@Action(value = "inventory_audit_init")
	public String auditInit(){
		return SUCCESS;
	}
	
	/**
	 * @desc 查询盘点审核单
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月20日,上午12:08:37
	 */
	@Action(value = "inventory_audit_view")
	public String auditView(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ServletActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		inventory.setHospitalId(admin.getEnterpriseId());
		pageBean = this.baseAllService.getInventoryManagerService().findInventoryAuditByPage(inventory, map);
		map.clear();
		map.put("total", pageBean.getTotalRow());
		map.put("data", pageBean.getResult());
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 跳转到审核页面
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月20日,上午1:21:32
	 */
	@Action(value = "inventory_audit", results = {@Result(name = SUCCESS, location = "inventory_audit.jsp")})
	public String audit(){
		return SUCCESS;
	}
	
	/**
	 * @desc 完成盘点，整理库存
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月20日,上午1:52:13
	 */
	@Action(value = "inventory_inventory")
	public String inventory(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(inventory.getId())){
			MedicalAdmin auditUser = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			List<MedicalInventoryDetail> inventoryDetailList = this.baseAllService.getInventoryManagerService().findInventoryDetailList(inventory.getId());
			this.baseAllService.getInStockManagerService().inventoryUpdateInStock(inventoryDetailList);
			this.baseAllService.getInventoryManagerService().updateinventoryStatus(SystemConfig.INVENTOR_STATUS_APPROVAL, inventory.getId());
			this.baseAllService.getInventoryManagerService().updateAuditUser(auditUser, inventory.getId());
			map.put("success", true);
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 不同意盘点，更改状态
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月20日,上午1:45:28
	 */
	@Action(value = "inventory_audit_reject")
	public String reject(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(inventory.getId())){
			MedicalAdmin auditUser = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			this.baseAllService.getInventoryManagerService().updateinventoryStatus(SystemConfig.INVENTOR_STATUS_REJECT, inventory.getId());
			this.baseAllService.getInventoryManagerService().updateAuditUser(auditUser, inventory.getId());
			map.put("success", true);
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 提交盘点审核
	 * @return null
	 * @author liy
	 * @datetime 2014年12月24日,上午11:25:20
	 */
	@Action(value = "inventory_to_audit")
	public String toAudit(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		boolean flag = this.baseAllService.getInventoryManagerService().toAudit(this.inventory.getId());
		if(flag){
			map.put("success", true);
		}else{
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	
	@Override
	public MedicalInventory getModel() {
		return inventory;
	}

}
